package edu.neusoft.android.yuejian14.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.activity.SearchTrainActivity;
import edu.neusoft.android.yuejian14.adapter.TrainListAdapter;
import edu.neusoft.android.yuejian14.javabean.Train;
import edu.neusoft.android.yuejian14.model.TrainModel;

public class TrainFragment extends Fragment implements View.OnClickListener,RetrofitListener{
    private RecyclerView recyclerView;
    private TrainListAdapter trainListAdapter;
    private ImageButton imageButtonSearch;
    private RollPagerView rollPV;
    private View rView;
    private ScheduledExecutorService scheduledExecutorService;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rView=inflater.inflate(R.layout.tab01, null);

        return rView;
    }

//设置轮播图
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        imageButtonSearch = (ImageButton) view.findViewById(R.id.btn_search_train);
        recyclerView = (RecyclerView) view.findViewById(R.id.home_recycler);

        initEvents();//点击事件处理
        initData();//网络获取


        rollPV = (RollPagerView) view.findViewById(R.id.rollPV);
        //设置每个图片的切换时间
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        rollPV.setAdapter(new TrainFragment.RollPagerAdapter());
    }

    //适配器
    private class RollPagerAdapter extends StaticPagerAdapter {

        private int images[] = {
                R.drawable.img1,
                R.drawable.img2,
                R.drawable.img3,
                R.drawable.img4
        };

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }
//

    private void initEvents() {
        imageButtonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(getActivity().getApplicationContext(), SearchTrainActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }


    private void initData() {
        TrainModel trainModel = new TrainModel();
        trainModel.gettrainlist(this);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        if (getActivity() != null){
            List<Train> trainBeanList = (List<Train>) object;
            trainListAdapter = new TrainListAdapter(getActivity(),trainBeanList);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(layoutManager);
            recyclerView.setAdapter(trainListAdapter);
            recyclerView.setItemAnimator(new DefaultItemAnimator());
        }
    }

    @Override
    public void onFail() {

    }

    @Override
    public void onClick(View view) {

    }

}
